package com.kgc.scd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kgc.scd.bean.NotifySeckill;

import com.kgc.scd.bean.Seckill;
import com.kgc.scd.bean.SeckillNotifyResult;
import com.kgc.scd.bean.SeckillResult;
import com.kgc.scd.mapper.NotifySeckillMapper;
import com.kgc.scd.mapper.SeckillMapper;
import com.kgc.scd.mapper.SeckillNotifyResultMapper;
import com.kgc.scd.service.SecKillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: com.kgc.scd.service.impl
 * @author: HarryXzy
 * @create: 2022-06-14 12:08
 * @description:
 **/
@Service
public class SecKillServiceImpl implements SecKillService {
    @Autowired(required = false)
    private SeckillMapper seckillMapper;
    @Autowired(required = false)
    private NotifySeckillMapper notifySeckillMapper;
    @Autowired(required = false)
    private SeckillNotifyResultMapper seckillNotifyResultMapper;


    @Override
    public List<SeckillResult> getAllSecKill() {
        // 获取当前时间
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis());
        List<SeckillResult> seckills = seckillMapper.selectAllSecKill(format);
        return seckills;
    }

    @Override
    public boolean userHaveNotify(String userId, String secKillid) {
        QueryWrapper<NotifySeckill> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",userId);
        wrapper.eq("seckill_pid",secKillid);
        return notifySeckillMapper.selectOne(wrapper) != null;
    }

    @Override
    public boolean canNotify(String secKillid) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        // 加5-6分钟
        Date afterDate = new Date(now .getTime() + 306000);
        String afterDateStr = sdf.format(afterDate);
        System.out.println(sdf.format(afterDate ));
        QueryWrapper<Seckill> wrapper = new QueryWrapper<>();
        wrapper.eq("seckill_id",secKillid);
        wrapper.gt("seckill_starttime",afterDateStr);
        return seckillMapper.selectOne(wrapper) != null;
    }

    @Override
    public void addNotify(String userId, String secKillid) {
        NotifySeckill notifySeckill = new NotifySeckill();
        notifySeckill.setUid(userId);
        notifySeckill.setSeckillPid(secKillid);
        notifySeckillMapper.insert(notifySeckill);
    }

    @Override
    public boolean secKillHaveEnd(String secKillid) {
        QueryWrapper<Seckill> wrapper = new QueryWrapper<>();
        wrapper.eq("seckill_id",secKillid);
        Seckill seckill = seckillMapper.selectOne(wrapper);
        Date seckillStarttime = seckill.getSeckillStarttime();
        // 活动开始时间
        long secKillSec = seckillStarttime.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 当前时间
        long currentTimeMillis = System.currentTimeMillis();

        return secKillSec < currentTimeMillis;

    }

    @Override
    public List<SeckillNotifyResult> getNowNeedNotify() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        // 加10分钟
        Date afterDate = new Date(now.getTime() + 300000);
        String nowAdd = sdf.format(afterDate);
        List<SeckillNotifyResult> seckillNotifyResults = seckillNotifyResultMapper.selectNowNeedNotify(nowAdd);
        System.out.println(nowAdd);
        return seckillNotifyResults;
    }

    @Override
    public void addSecKill(Seckill seckill) {
        seckillMapper.insert(seckill);
    }

    @Override
    public SeckillResult getSecKillResultById(String seckillId) {
       return seckillMapper.selectSecKillResultById(seckillId);
    }


}
